<?php

namespace app\api\model\curriculum;

use app\api\model\Models;
use think\db\Query;
use app\api\traits\model\curriculum\Curriculum as CurriculumTraits;

class Curriculum extends Models
{
    use CurriculumTraits;

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $createTime = 'create_at';

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $updateTime = 'update_at';

    /**
     * 设置时间戳格式
     * @var string
     */
    protected $autoWriteTimestamp = 'datetime';

    /*
     * 定义全局的查询范围
     * @var array
     */
    protected $globalScope = ['noDisplay'];

    /**
     * 根据类型获取考试类型
     * @param Query $query
     * @param int $type
     * @return void
     */
    public function scopeCategory( Query $query, int $category )
    {
        $query->where('category', $category);
    }

    /**
     * 设置全局不显示字段
     * @param Query $query
     * @return void
     */
    public function scopeNoDisplay( Query $query )
    {
        $query->field('create_at,update_at', true);
    }

    /**
     * 设置类型获取器
     * @return string
     */
    protected function getCategoryTextAttr( $value, $data )
    {
        $category = config('field.curriculum.category');
        return $category[$data['category']]??'暂无';
    }

    /**
     * 设置类别获取器
     * @return string
     */
    protected function getTypeTextAttr( $value, $data )
    {
        $category = config('field.curriculum.type');
        return $category[$data['type']]??'暂无';
    }

    /**
     * 格式化开始周期
     * @return string
     */
    protected function getStartCycleTextAttr( $value, $data )
    {
        return date("Y.m.d", strtotime($data['start_cycle']));
    }

    /**
     * 格式化结束周期
     * @return string
     */
    protected function getEndCycleTextAttr( $value, $data )
    {
        return date("Y.m.d", strtotime($data['end_cycle']));
    }

    /**
     * 解析富文本
     * @return string
     */
    protected function getDetailHtmlAttr( $value, $data )
    {
        return htmlspecialchars_decode($data['detail']);
    }

}
